package com.punai.prodatalibrary.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.punai.common.constant.HttpStatus;
import com.punai.common.core.page.TableDataInfo;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.EcoCbsummary;
import com.punai.externalccb.mapper.EcoCbsummaryMapper;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.prodatalibrary.domain.LibProject;
import com.punai.prodatalibrary.mapper.LibProjectMapper;
import com.punai.prodatalibrary.service.ILibProjectService;

import java.util.Objects;

import static com.punai.common.utils.PageUtils.startPage;

/**
 * 项目主表Service业务层处理
 *
 * @author tj
 * @date 2023-03-31
 */
@Service
public class LibProjectServiceImpl extends ServiceImpl<LibProjectMapper,LibProject> implements ILibProjectService
{
    @Autowired
    private LibProjectMapper libProjectMapper;
    @Autowired
    private ILibProjectInfoService libProjectInfoService;
    @Autowired
    private EcoCbsummaryMapper ecoCbsummaryMapper;

    /**
     * 查询项目主表
     *
     * @param id 项目主表主键
     * @return 项目主表
     */
    @Override
    public LibProject selectLibProjectById(String id)
    {
        return libProjectMapper.selectLibProjectById(id);
    }

    /**
     * 查询项目主表列表
     *
     * @param libProject 项目主表
     * @return 项目主表
     */
    @Override
    public List<LibProject> selectLibProjectList(LibProject libProject)
    {
        return libProjectMapper.selectLibProjectList(libProject);
    }

    @Override
    public TableDataInfo selectProjectInfoList(GlobalProInfoBo bo) {
        List<GlobalProInfoBo> infoList = libProjectInfoService.selectInfoList(bo);
        for (GlobalProInfoBo infoBo : infoList) {
            if (StrUtil.equalsAny(bo.getStagetype(),
                    CCBConstants.CCB_TYPE.MBZRCB.getCode(),
                    CCBConstants.CCB_TYPE.FGSMBCB.getCode(),
                    CCBConstants.CCB_TYPE.YDCBFX.getCode(),
                    CCBConstants.CCB_TYPE.JGCBFX.getCode()))
            {
                List<EcoCbsummary> cbsummaryList = ecoCbsummaryMapper.selectEcoCbsummaryList(new EcoCbsummary(1, infoBo.getId(), CCBConstants.CCB_TYPE.MBZRCB.getCode(),0,"合计"));
                BigDecimal ysTotal = convertDecimal(cbsummaryList.stream().map(EcoCbsummary::getYstotal).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));//标前成本/合同额
                BigDecimal cbTotal = convertDecimal(cbsummaryList.stream().map(EcoCbsummary::getTargetnottotal).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));//目标成本
                if (ysTotal!=null && cbTotal!=null){
                    infoBo.setBqcb(ysTotal);
                    infoBo.setHtje(ysTotal);
                    infoBo.setMbjhcb(cbTotal);
                    //利润率 = 收入-成本/收入
                    BigDecimal lre = ysTotal.subtract(cbTotal);
                    if (ysTotal.compareTo(BigDecimal.ZERO) == 0) {
                        infoBo.setMbjhlrl(new BigDecimal("0.00"));
                    } else {
                        infoBo.setMbjhlrl(lre.divide(ysTotal, 2, BigDecimal.ROUND_HALF_UP));
                    }
                }
            }
        }
        Integer total = libProjectInfoService.selectInfoListTotal(bo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(infoList);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 转换成 万元
     * @param data
     * @return
     */
    public static BigDecimal convertDecimal(BigDecimal data){
        return data != null ? data.divide(new BigDecimal(10000)).setScale(3,BigDecimal.ROUND_HALF_UP) : null;
    }

    /**
     * 新增项目主表
     *
     * @param libProject 项目主表
     * @return 结果
     */
    @Override
    public int insertLibProject(LibProject libProject)
    {
            return libProjectMapper.insert(libProject);
    }

    /**
     * 修改项目主表
     *
     * @param libProject 项目主表
     * @return 结果
     */
    @Override
    public int updateLibProject(LibProject libProject)
    {
        return libProjectMapper.updateById(libProject);
    }

    /**
     * 批量删除项目主表
     *
     * @param ids 需要删除的项目主表主键
     * @return 结果
     */
    @Override
    public int deleteLibProjectByIds(String[] ids)
    {
        return libProjectMapper.deleteLibProjectByIds(ids);
    }

    /**
     * 删除项目主表信息
     *
     * @param id 项目主表主键
     * @return 结果
     */
    @Override
    public int deleteLibProjectById(String id)
    {
        return libProjectMapper.deleteLibProjectById(id);
    }

    @Override
    public LibProject findByCode(String proCode) {
        return libProjectMapper.selectLibProjectByCode(proCode);
    }
}
